//
//  YWInformationBankTool.m
//  maintain
//
//  Created by Kyo on 16/5/19.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWInformationBankTool.h"
#import "YWDBTool.h"
#import "WMHttpTool.h"
#import "MJExtension.h"
#import "YWCommonTool.h"
#import "YWInformationBankModel.h"
#import "YWMacro.h"
#import "YWConst.h"

@implementation YWInformationBankTool
YWSingletonM(Manager)

-(void)requestInformation:(NSTimeInterval)createTime WithComplete:(void (^)(BOOL))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    
    NSDictionary *params = @{@"createTime":@(createTime)};
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool informatiomBankUrl] params:createTime?params:nil success:^(NSHTTPURLResponse* response, id json) {
        NSArray* informationArray = [YWInformationBankModel mj_objectArrayWithKeyValuesArray:json];
        
        [[YWInformationBankTool shareManager] saveInformationWithArray:informationArray];
        
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(NO);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"请求失败,稍后请重试"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (BOOL)saveInformationWithArray:(NSArray *)informationArray{
    return [[YWDBTool shareDBTool] saveInformationWithArray:informationArray];
}

-(NSArray *)informationArrayWithPage:(NSInteger)page{
    return [[YWDBTool shareDBTool] informationArrayWithPage:page];
}
@end
